#include "stdafx.h"
#include "Bloom.h"

Bloom::Bloom() : cObject()
{
	//...
}

void Bloom::Init(char *szFileNFG, char *fileTGA)
{
	char **fileName = new char*[3];
	fileName[0] = fileTGA;
	fileName[1] = "../Resources/Textures/Woman1.tga";
	fileName[2] = "../Resources/Textures/grass.tga";
	cObject::Init(GL_TEXTURE_2D, fileName, 3, szFileNFG);

	//Initshader
	m_Shader = BloomShader();
	m_Shader.Init("../Resources/Shaders/BloomShaderVS.vs", "../Resources/Shaders/BloomShaderFS.fs");
}

float a = 0;
void Bloom::Draw(ESContext *esContext)
{
	//Binding to vertex
	glUseProgram(m_Shader.program);
	glBindBuffer(GL_ARRAY_BUFFER, m_Model->GetVertexID());
	if(m_Shader.VertPosition != -1) //=>Position
	{
		glEnableVertexAttribArray(m_Shader.VertPosition);
		glVertexAttribPointer(m_Shader.VertPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
	}
	if(m_Shader.VertTexCoord != -1)	//=>TexCoordinate (uv)
	{
		glEnableVertexAttribArray(m_Shader.VertTexCoord);
		glVertexAttribPointer(m_Shader.VertTexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3));
	}
	if(m_Shader.VertNorm != -1) //=> Normmal
	{
		glEnableVertexAttribArray(m_Shader.VertNorm);
		glVertexAttribPointer(m_Shader.VertNorm, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3) + sizeof(Vector2));
	}

	//Binding to Matrix
	if(m_Shader.WVPMatrix != -1) //=>World View Projection 
	{
		Matrix maViewProjection = Globals::MainCamera->GetVPMatrix();
		glUniformMatrix4fv(m_Shader.WVPMatrix, 1, GL_FALSE, (GLfloat*)&(m_maLocalW * maViewProjection).m[0][0]);
	}
	if(m_Shader.WMatrix != -1) //World (local)
	{
		glUniformMatrix4fv(m_Shader.WMatrix, 1, GL_FALSE, (GLfloat*)&(m_maLocalW.m[0][0]));
	}

	//Binding global position
	if(m_Shader.CameraPosition != -1) //Camera Position
	{
		Vector3 cameraPos = Globals::MainCamera->GetPosition();
		glUniform3f(m_Shader.CameraPosition, cameraPos.x, cameraPos.y, cameraPos.z);
	}
	if(m_Shader.LightPosition != -1) //Light Position
	{
		glUniform3f(m_Shader.LightPosition, 0.0f, 150.0f, 50.0f);
	}

	//Binding to texture
	//	Texture[0]: Origin Texture
	//	Texture[1]: Blurred Texture
	//	Texture[2]: Depth Texture
	GLint textureCount = 3;
	for(int i=0; i<textureCount; i++)
	{
		glActiveTexture(GL_TEXTURE0+i);
		glBindTexture(GL_TEXTURE_2D, m_Texture->GetTextureID()[i]);
	}

	if(m_Shader.Limit != -1)
	{
		a+=0.001f;
		glUniform1f(m_Shader.Limit, 0.5f);
	}
	if(m_Shader.Near != -1)
	{
		glUniform1f(m_Shader.Near, 5.0f);
	}
	if(m_Shader.Far != -1)
	{
		glUniform1f(m_Shader.Far, 10.0f);
	}
	if(m_Shader.Fade != -1)
	{
		glUniform1f(m_Shader.Fade, 5.0f);
	}
	if(m_Shader.Clarity != -1)
	{
		glUniform1f(m_Shader.Clarity, 15.0f);
	}

	if(m_Shader.Step != -1)
	{
		float k = 1.0f;
		float x = 1.0f / Globals::screenWidth;
		float y = 1.0f / Globals::screenHeight;
		float z = sqrt(2.0f) / 2.0f * x;
		float w = sqrt(2.0f) / 2.0f * y;
		glUniform4f(m_Shader.Step, x, y, z, w);
	}

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Model->GetIndiceID());
	glDrawElements(GL_TRIANGLES, m_Model->GetIndiceNumber(), GL_UNSIGNED_INT, 0);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
}

Bloom::~Bloom()
{
}